package com.ruoyi.wms.service.impl;

import java.util.List;

import com.ruoyi.wms.domain.WmsProductstock;
import com.ruoyi.wms.mapper.WmsProductstockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.WmsProductIncomeMapper;
import com.ruoyi.wms.domain.WmsProductIncome;
import com.ruoyi.wms.service.IWmsProductIncomeService;
import com.ruoyi.common.core.text.Convert;

/**
 * 产品入库Service业务层处理
 * 
 * @author chao
 * @date 2024-07-24
 */
@Service
public class WmsProductIncomeServiceImpl implements IWmsProductIncomeService 
{
    @Autowired
    private WmsProductIncomeMapper wmsProductIncomeMapper;

    @Autowired
    private WmsProductstockMapper wmsProductstockMapper;

    /**
     * 查询产品入库
     * 
     * @param id 产品入库主键
     * @return 产品入库
     */
    @Override
    public WmsProductIncome selectWmsProductIncomeById(Long id)
    {
        return wmsProductIncomeMapper.selectWmsProductIncomeById(id);
    }

    /**
     * 查询产品入库列表
     * 
     * @param wmsProductIncome 产品入库
     * @return 产品入库
     */
    @Override
    public List<WmsProductIncome> selectWmsProductIncomeList(WmsProductIncome wmsProductIncome)
    {
        return wmsProductIncomeMapper.selectWmsProductIncomeList(wmsProductIncome);
    }

    /**
     * 新增产品入库
     * 
     * @param wmsProductIncome 产品入库
     * @return 结果
     */
    @Override
    public int insertWmsProductIncome(WmsProductIncome wmsProductIncome)
    {
        return wmsProductIncomeMapper.insertWmsProductIncome(wmsProductIncome);
    }

    /**
     * 修改产品入库
     * 
     * @param wmsProductIncome 产品入库
     * @return 结果
     */
    @Override
    public int updateWmsProductIncome(WmsProductIncome wmsProductIncome)
    {
        return wmsProductIncomeMapper.updateWmsProductIncome(wmsProductIncome);
    }

    /**
     * 批量删除产品入库
     * 
     * @param ids 需要删除的产品入库主键
     * @return 结果
     */
    @Override
    public int deleteWmsProductIncomeByIds(String ids)
    {
        return wmsProductIncomeMapper.deleteWmsProductIncomeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除产品入库信息
     * 
     * @param id 产品入库主键
     * @return 结果
     */
    @Override
    public int deleteWmsProductIncomeById(Long id)
    {
        return wmsProductIncomeMapper.deleteWmsProductIncomeById(id);
    }

    @Override
    public void Productstorge(Long RowId,Long productId, Double num) {
        WmsProductstock wmsProductstock = wmsProductstockMapper.selectWmsProductstockByProductId(productId);
        if(wmsProductstock == null){
            WmsProductIncome wmsProductIncome = wmsProductIncomeMapper.selectWmsProductIncomeById(RowId);

            WmsProductstock Productstock = new WmsProductstock();
            Productstock.setProductId(wmsProductIncome.getProductId());
            Productstock.setName(wmsProductIncome.getName());
            Productstock.setModel(wmsProductIncome.getModel());
            Productstock.setNum(wmsProductIncome.getNum());
            Productstock.setUnitName(wmsProductIncome.getUnitName());

            wmsProductstockMapper.insertWmsProductstock(Productstock);
        }
        else{
            wmsProductstockMapper.excuteProductStorge(productId,num);
        }
        wmsProductIncomeMapper.updateStorgeStatusInfo(RowId);
    }
}
